package com.jy.adscm.operation.cg.controller;


import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgSuppilerInfo;
import com.jy.adscm.operation.cg.service.CgSuppilerInfoService;
import com.jy.adscm.operation.cg.service.CgSuppilerInfoService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-06
 */
@RestController
@RequestMapping("adscm/operation/cg/cgSuppilerInfo")
public class CgSuppilerInfoController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    CgSuppilerInfoService  cgSuppilerInfoService;

    /**
     * 增加供应商信息
     * @param cgSuppilerInfo
     * @param request
     * @return
     */
    @AdscmLogger("增加供应商欠票与欠款信息")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody CgSuppilerInfo cgSuppilerInfo, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        cgSuppilerInfo.setCreator(token.getUser().getId().toString());
        boolean insert = cgSuppilerInfoService.insertCgSuppilerInfo(cgSuppilerInfo);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("selectBySid/{sid}/{type}")
    public SimpleCoreResponse selectBySid( @PathVariable Integer sid ,@PathVariable Integer type, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
       List<CgSuppilerInfo> cgSuppilerInfos= cgSuppilerInfoService.selectBySid(sid,type);
        return  new SimpleCoreResponse("200","新增成功",cgSuppilerInfos);
    }
}

